﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client.ApplicationServices;

namespace MyLiveMesh.ViewModels
{
    public class LoginPanel : Abstract
    {
        bool m_loginVisibility;
        bool m_logoutVisibility;
        string m_email;

        public bool LoginVisibility
        {
            get
            {
                return (m_loginVisibility);
            }
            set
            {
                if (m_loginVisibility != value)
                {
                    m_loginVisibility = value;
                    this.OnPropertyChanged("LoginVisibility");
                }
            }
        }
        public bool LogoutVisibility
        {
            get
            {
                return (m_logoutVisibility);
            }
            set
            {
                if (m_logoutVisibility != value)
                {
                    m_logoutVisibility = value;
                    this.OnPropertyChanged("LogoutVisibility");
                }
            }
        }
        public ICommand CommandDisplayLoginForm
        {
            get
            {
                return (new DisplayLoginFormCommand(this));
            }
        }
        public ICommand CommandLogout
        {
            get
            {
                return (new LogoutCommand(this));
            }
        }
        public ICommand CommandDisplayEditForm
        {
            get
            {
                return (new DisplayEditFormCommand(this));
            }
        }
        public ICommand CommandDisplayRegister
        {
            get
            {
                return (new DisplayRegisterCommand(this));
            }
        }
        public string Email
        {
            get
            {
                return (m_email);
            }
            set
            {
                if (m_email != value)
                {
                    m_email = value;
                    OnPropertyChanged("Email");
                }
            }
        }

        public LoginPanel()
        {
            LogoutVisibility = WebContext.Current.User.IsAuthenticated;
            LoginVisibility = !LogoutVisibility;
        }
    }

    public class DisplayLoginFormCommand : ICommand
    {
        LoginPanel m_loginPanel;

        public DisplayLoginFormCommand(LoginPanel _loginPanel)
        {
            m_loginPanel = _loginPanel;
        }

        #region ICommand Membres

        public bool CanExecute(object parameter)
        {
            return (true);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            WebContext.Current.Authentication.LoggedIn += new EventHandler<AuthenticationEventArgs>(loggedIn_event);
            var login = new MyLiveMesh.Views.Login();
            login.Show();
        }

        void loggedIn_event(object sender, AuthenticationEventArgs e)
        {
            WebContext.Current.Authentication.LoggedIn -= loggedIn_event;
            m_loginPanel.LoginVisibility = false;
            m_loginPanel.LogoutVisibility = true;
            m_loginPanel.Email = WebContext.Current.User.Name;
        }

        #endregion
    }

    public class LogoutCommand : ICommand
    {
        LoginPanel m_loginPanel;

        public LogoutCommand(LoginPanel _loginPanel)
        {
            m_loginPanel = _loginPanel;
        }

        #region ICommand Membres

        public bool CanExecute(object parameter)
        {
            return (true);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            WebContext.Current.Authentication.LoggedOut += new EventHandler<AuthenticationEventArgs>(loggedOut_event);
            WebContext.Current.Authentication.Logout(true);
        }

        void loggedOut_event(object sender, AuthenticationEventArgs e)
        {
            WebContext.Current.Authentication.LoggedOut -= loggedOut_event;
            m_loginPanel.LoginVisibility = true;
            m_loginPanel.LogoutVisibility = false;
        }

        #endregion
    }

    public class DisplayRegisterCommand : ICommand
    {
        LoginPanel m_loginPanel;

        public DisplayRegisterCommand(LoginPanel _loginPanel)
        {
            m_loginPanel = _loginPanel;
        }

        #region ICommand Membres

        public bool CanExecute(object parameter)
        {
            return (true);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            ChildWindow window = new MyLiveMesh.Views.Register();
            window.Show();
        }

        #endregion
    }

    public class DisplayEditFormCommand : ICommand
    {
        LoginPanel m_loginPanel;

        public DisplayEditFormCommand(LoginPanel _loginPanel)
        {
            m_loginPanel = _loginPanel;
        }

        #region ICommand Membres

        public bool CanExecute(object parameter)
        {
            return (true);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            ChildWindow window = new MyLiveMesh.Views.EditAccount();
            window.Show();
        }

        #endregion
    }

}
